﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Media;
//using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace Dart
{
    
    public partial class FormDart : Form
    {
        private int maxPlayers = 8;
        public int currentPlayer;
        private int playerCount = 0;
        private Boolean hitMember = false;
        private int ranking = 1;
        private int[] currentScore = new int[16];
        private int[] currentTopScore = new int[16];
        private int[] currentTopScore2 = new int[16];
        private Boolean[] playerFinished = new Boolean[16];
        private int currentRound;
        private int currentDart;
        private int[,,] score = new int[16,1000,4];
        public Label[] lblPlayerArray = new Label[16];
        public Label[] lblPlayerScoreArray = new Label[16];
        private Label[] lblPlayerAvgArray = new Label[16];
        private Label[] lblPlayerAvg2Array = new Label[16];
        private Label[] lblPlayerTopScoreArray = new Label[16];
        private Label[] lblPlayerTopScore2Array = new Label[16];

        private Label[,,] lblPlayerCricketArray = new Label[16,7,4];

        public Boolean finish = false;
        
        public Button[] buttonScoreArray = new Button[1000];
        private int[] halfItRounds = new int[21];
        private int[] halfItRoundsX = new int[21];
        private int halfItMaxRounds = 20;

        private Thread thread;

        DartBoard dartsBoard;

        FormScore formScore = new FormScore();

        public FormDart()
        {
        }

        public FormDart(Boolean isNewForm)
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(this.FormDart_FormClosing);
            dartsBoard = new DartBoard(this);

        }

        private void FormDart_FormClosing(object sender, FormClosingEventArgs e)
        {
            dataSet.WriteXml("data.xml");
        }

        private void FormDart_Resize(object sender, System.EventArgs e)
        {
            //this.panelSettings.Left = this.Size.Width - this.panelSettings.Width - 20;
            this.resizePlayers();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.buttonStart.Enabled = false;
            createInputs();
            scoreButtons(false);
            dataSet.ReadXml("data.xml");
            this.formScore.Resize += new System.EventHandler(this.FormDart_Resize);
        }

        private void buttonStart_Click_1(object sender, EventArgs e)
        {
            this.buttonReset.Enabled = true;
            this.buttonShowDartBoard.Enabled = true;

            currentPlayer = 1;
            currentRound = 1;
            currentDart = 1;
            playerFinished = new Boolean[16];
            score = new int[16, 1000, 4];
            finish = false;

            formScore.getRoundLabel().Text = "Runde 1";

            formScore.getLabelDart1().ForeColor = Color.Red;
            formScore.getLabelDart2().ForeColor = Color.Red;
            formScore.getLabelDart3().ForeColor = Color.Red;

            Random random = new Random();

            
            if (this.comboBoxGame.Text == "Half-It 10")
            {
                int check = 0;
                this.halfItMaxRounds = 10;
                for (int i = 0; i < 10; )
                {
                    check = random.Next(11, 21);
                    while (!(this.halfItRounds.Contains(check)))
                    {
                        this.halfItRounds[i] = check;
                        i++;
                    }
                }
            }
            else if (this.comboBoxGame.Text == "Half-It 10 V2")
            {
                int check = 0;
                this.halfItMaxRounds = 10;
                for (int i = 0; i < 10; )
                {
                    check = random.Next(11, 21);
                    while (!(this.halfItRounds.Contains(check)))
                    {
                        this.halfItRounds[i] = check;
                        i++;
                    }
                }
                for (int i = 0; i < 10; i++)
                {
                    check = random.Next(1, 4);
                    this.halfItRoundsX[i] = check;
                }
            }
            else if (this.comboBoxGame.Text == "Half-It 20")
            {
                int check = 0;
                this.halfItMaxRounds = 20;
                for (int i = 0; i < 20; )
                {
                    check = random.Next(1, 21);
                    while (!(this.halfItRounds.Contains(check)))
                    {
                        this.halfItRounds[i] = check;
                        i++;
                    }
                }
                this.halfItRounds[20] = 25;
            }

            for (int i = 1; i < playerCount; i++)
            {
                if (this.comboBoxGame.Text == "A-Cricket")
                {
                    currentScore[i] = 0;
                }
                else if (this.comboBoxGame.Text == "Half-It 10" || this.comboBoxGame.Text == "Half-It 10 V2" || this.comboBoxGame.Text == "Half-It 20")
                {
                    currentScore[i] = 0;
                    formScore.getLabelInfo().Visible = true;
                }
                else if (this.comboBoxGame.Text == "301 Scream")
                {
                    currentScore[i] = 0;
                    this.formScore.getLabelInfo().Visible = true;
                    this.formScore.getLabelInfo().Font = new System.Drawing.Font("Comic Sans MS", 28F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 

                    //this.formScore.getLabelInfo2().Visible = true;
                    //formScore.getLabelInfo().Font = new Font(lblPlayerArray[i].Font.FontFamily, 30, lblPlayerArray[i].Font.Style);
                }
                else
                {
                    currentScore[i] = Convert.ToInt16(this.comboBoxGame.Text);
                    if (lblPlayerAvgArray[i - 1] != null)
                    {
                        lblPlayerAvgArray[i - 1].Text = "0";
                        lblPlayerAvg2Array[i - 1].Text = "0";
                    }
                    lblPlayerScoreArray[i - 1].Text = this.comboBoxGame.Text;
                }
                
                this.refreshData();
            }

            this.refreshData();
            scoreButtons(true);
        }
        
        private void buttonSettings_Click(object sender, EventArgs e)
        {
            if (this.panelSesttings.Visible)
            {
                this.panelSesttings.Visible = false;
                dataSet.WriteXml("data.xml");
            }
            else
            {
                this.panelSesttings.Visible = true;
            }
        }

        private void buttonReset_Click_1(object sender, EventArgs e)
        {
            currentScore = new int[16];
            playerFinished = new Boolean[16];
            currentPlayer = 1;
            currentRound = 1;
            currentDart = 1;
            ranking = 1;
            playerFinished = new Boolean[16];
            score = new int[16, 1000, 4];

            formScore.getRoundLabel().Text = "Runde 1";

            formScore.getLabelDart1().ForeColor = Color.Red;
            formScore.getLabelDart2().ForeColor = Color.Red;
            formScore.getLabelDart3().ForeColor = Color.Red;

            for (int i = 0; i < playerCount; i++)
            {
                formScore.Controls.Remove(lblPlayerArray[i]);
                formScore.Controls.Remove(lblPlayerScoreArray[i]);
                formScore.Controls.Remove(lblPlayerAvgArray[i]);
                formScore.Controls.Remove(lblPlayerAvg2Array[i]);
                formScore.Controls.Remove(lblPlayerTopScoreArray[i]);
                formScore.Controls.Remove(lblPlayerTopScore2Array[i]);
                for (int j = 0; j < 7; j++)
                {
                    formScore.Controls.Remove(lblPlayerCricketArray[i, j, 0]);
                    formScore.Controls.Remove(lblPlayerCricketArray[i, j, 1]);
                    formScore.Controls.Remove(lblPlayerCricketArray[i, j, 2]);
                    formScore.Controls.Remove(lblPlayerCricketArray[i, j, 3]);
                }
            }
            playerCount = 0;
            formScore.getRoundLabel().Text = "";
            for (int i = 1; i < currentScore.Length; i++)
            {
                if (this.comboBoxGame.Text == "A-Cricket" || this.comboBoxGame.Text.StartsWith("Half-It") == true)
                {
                    currentScore[i] = 0;
                }
                else
                {
                    currentScore[i] = Convert.ToInt16(this.comboBoxGame.Text);
                }
                if (lblPlayerAvgArray[i - 1] != null)
                {
                    lblPlayerAvgArray[i - 1].Text = "0";
                    lblPlayerAvg2Array[i - 1].Text = "0";
                    lblPlayerTopScoreArray[i - 1].Text = "0";
                    lblPlayerTopScore2Array[i - 1].Text = "0";
                }
                this.refreshData();
            }
            this.comboBoxPlayer.Enabled = true;
            this.buttonStart.Enabled = false;
            this.comboBoxGame.Enabled = true;
            this.comboBoxGameOption1.Enabled = true;
            this.comboBoxGameOptionIn.Enabled = true;
            this.comboBoxGameOptionOut.Enabled = true;
            this.buttonUndo.Enabled = false;
            this.buttonShowDartBoard.Enabled = false;
            this.buttonReset.Enabled = false;

            this.dartsBoard.Hide();

            this.scoreButtons(false);
        }

        private void buttonSaveAndClose_Click(object sender, EventArgs e)
        {
            this.panelSesttings.Visible = false;
            dataSet.WriteXml("data.xml");
        }

        private void buttonAddPlayer_Click(object sender, EventArgs e)
        {
            if (!formScore.Visible)
            {
                formScore.Show(this);
            }

            addPlayer(playerCount + 1, this.comboBoxGame.Text, this.comboBoxPlayer.Text, this.comboBoxPlayer.Tag.ToString());
            if (playerCount == maxPlayers)
            {
                this.comboBoxPlayer.Enabled = false;
            }

            this.buttonReset.Enabled = true;
        }

        private void buttonShowDartBoard_Click(object sender, EventArgs e)
        {
            dartsBoard.Show(this);
        }

        private void resizePlayers()
        {
            int tempTop = lblPlayerArray[0].Top;
            int tempLeft = lblPlayerArray[0].Left;
            int playerMaxWidth = this.formScore.Width;
            int playerMaxHeight = this.formScore.Height / (this.playerCount + 2);

            for (int i = 0; i < lblPlayerArray.Length; i++)
            {
                if (lblPlayerArray[i] != null && lblPlayerScoreArray[i] != null)
                {
                    lblPlayerArray[i].Width = playerMaxWidth / 2;
                    lblPlayerArray[i].Height = playerMaxHeight;
                    lblPlayerArray[i].Font = new Font(lblPlayerArray[i].Font.FontFamily, playerMaxHeight / 2, lblPlayerArray[i].Font.Style);
                    lblPlayerArray[i].Top = tempTop + (playerMaxHeight * i) + (i * 10);

                    lblPlayerScoreArray[i].Left = lblPlayerArray[i].Left + lblPlayerArray[i].Width + 10;
                    lblPlayerScoreArray[i].Width = playerMaxWidth / 4;
                    lblPlayerScoreArray[i].Height = playerMaxHeight;
                    lblPlayerScoreArray[i].Font = new Font(lblPlayerArray[i].Font.FontFamily, playerMaxHeight / 2, lblPlayerArray[i].Font.Style);
                    lblPlayerScoreArray[i].Top = tempTop + (playerMaxHeight * i) + (i * 10);

                    lblPlayerAvgArray[i].Left = lblPlayerScoreArray[i].Left + lblPlayerScoreArray[i].Width + 10;
                    lblPlayerAvgArray[i].Width = playerMaxWidth / 10;
                    lblPlayerAvgArray[i].Height = playerMaxHeight / 2 - 5;
                    lblPlayerAvgArray[i].Font = new Font(lblPlayerArray[i].Font.FontFamily, playerMaxHeight / 4, lblPlayerArray[i].Font.Style);
                    lblPlayerAvgArray[i].Top = tempTop + (playerMaxHeight * i) + (i * 10);

                    lblPlayerAvg2Array[i].Left = lblPlayerAvgArray[i].Left + lblPlayerAvgArray[i].Width + 10;
                    lblPlayerAvg2Array[i].Width = playerMaxWidth / 10;
                    lblPlayerAvg2Array[i].Height = playerMaxHeight / 2 - 5;
                    lblPlayerAvg2Array[i].Font = new Font(lblPlayerArray[i].Font.FontFamily, playerMaxHeight / 4, lblPlayerArray[i].Font.Style);
                    lblPlayerAvg2Array[i].Top = tempTop + (playerMaxHeight * i) + (i * 10);

                    lblPlayerTopScoreArray[i].Left = lblPlayerScoreArray[i].Left + lblPlayerScoreArray[i].Width + 10;
                    lblPlayerTopScoreArray[i].Width = playerMaxWidth / 10;
                    lblPlayerTopScoreArray[i].Height = playerMaxHeight / 2 - 5;
                    lblPlayerTopScoreArray[i].Font = new Font(lblPlayerArray[i].Font.FontFamily, playerMaxHeight / 4, lblPlayerArray[i].Font.Style);
                    lblPlayerTopScoreArray[i].Top = tempTop + (playerMaxHeight * i) + (i * 10) + (playerMaxHeight / 2) + 5;

                    lblPlayerTopScore2Array[i].Left = lblPlayerAvgArray[i].Left + lblPlayerAvgArray[i].Width + 10;
                    lblPlayerTopScore2Array[i].Width = playerMaxWidth / 10;
                    lblPlayerTopScore2Array[i].Height = playerMaxHeight / 2 - 5;
                    lblPlayerTopScore2Array[i].Font = new Font(lblPlayerArray[i].Font.FontFamily, playerMaxHeight / 4, lblPlayerArray[i].Font.Style);
                    lblPlayerTopScore2Array[i].Top = tempTop + (playerMaxHeight * i) + (i * 10) + (playerMaxHeight / 2) + 5;

                }
            }
        }

        private void addPlayer(int playerNo, String game, string name, string id)
        {
            int top = 120;
            int playerMaxWidth = this.Width - this.panelSettings.Width;
            int playerMaxHeight = this.Height / (playerCount + 1);
            if (game == "A-Cricket" || game == "Half-It 10" || game == "Half-It 10 V2" || game == "Half-It 20")
            {
                currentScore[playerNo] = 0;
            }
            else if (game == "301 Scream")
            {
                currentScore[playerNo] = 0;
            }
            else
            {
                currentScore[playerNo] = Convert.ToInt16(game);
            }

            // Player Name
            var lblPlayer = new Label();
            lblPlayer.ForeColor = System.Drawing.Color.Gray;
            lblPlayer.BackColor = System.Drawing.Color.Black;
            lblPlayer.Font = new System.Drawing.Font("Comic Sans MS", 38F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            lblPlayer.ForeColor = System.Drawing.Color.Gray;
            lblPlayer.Location = new System.Drawing.Point(12, top + 9 + 117 * (playerNo-1));
            lblPlayer.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            lblPlayer.Name = name + playerNo;
            lblPlayer.Size = new System.Drawing.Size(280, 108);
            lblPlayer.Text = name;
            lblPlayer.Tag = id;
            lblPlayerArray[playerNo - 1] = lblPlayer;
            this.formScore.Controls.Add(lblPlayer);

            // Player Score
            var lblPlayerScore = new Label();
            lblPlayerScore.BackColor = System.Drawing.Color.Black;
            lblPlayerScore.ForeColor = System.Drawing.Color.Gray;
            lblPlayerScore.Font = new System.Drawing.Font("Comic Sans MS", 57F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            lblPlayerScore.Location = new System.Drawing.Point(lblPlayer.Left + lblPlayer.Width + 5, top + 9 + 117 * (playerNo - 1));
            lblPlayerScore.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            lblPlayerScore.Name = "labelScorePlayer" + playerNo;
            lblPlayerScore.Size = new System.Drawing.Size(193, 108);
            lblPlayerScore.Text = currentScore[playerNo].ToString();
            lblPlayerScoreArray[playerNo - 1] = lblPlayerScore;
            ToolTip t = new ToolTip();
            t.SetToolTip(lblPlayerScore, "Punkte Spieler " + playerNo);
            this.formScore.Controls.Add(lblPlayerScore);

            if (game == "A-Cricket")
            {
                
                for (int i=0;i<7;i++)
                {
                    var lblCricket1 = new Label();
                    lblCricket1.Location = new System.Drawing.Point(lblPlayerScore.Left + lblPlayerScore.Width + 5 + (29 * i), top + 9 + (117 * (playerNo - 1)));
                    lblCricket1.Size = new System.Drawing.Size(27, 25);
                    if (i == 6)
                    {
                        lblCricket1.Text = "B";
                    }
                    else
                    {
                        lblCricket1.Text = "" + (i + 15);
                    }
                    lblCricket1.Font = new System.Drawing.Font("Comic Sans MS", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    lblCricket1.Margin = new Padding(0);
                    lblCricket1.Padding = new Padding(0);
                    lblCricket1.BackColor = System.Drawing.Color.Black;
                    lblCricket1.ForeColor = System.Drawing.Color.Lime;
                    lblCricket1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
                    lblPlayerCricketArray[playerNo - 1, i, 0] = lblCricket1;
                    this.formScore.Controls.Add(lblCricket1);

                    var lblCricket2 = new Label();
                    lblCricket2.Location = new System.Drawing.Point(lblPlayerScore.Left + lblPlayerScore.Width + 5 + (29 * i), top + 37 + (117 * (playerNo - 1)));
                    lblCricket2.Size = new System.Drawing.Size(27, 25);
                    lblCricket2.BackColor = System.Drawing.Color.Black;
                    lblPlayerCricketArray[playerNo - 1, i, 1] = lblCricket2;
                    this.formScore.Controls.Add(lblCricket2);

                    var lblCricket3 = new Label();
                    lblCricket3.Location = new System.Drawing.Point(lblPlayerScore.Left + lblPlayerScore.Width + 5 + (29 * i), top + 65 + (117 * (playerNo - 1)));
                    lblCricket3.Size = new System.Drawing.Size(27, 25);
                    lblCricket3.BackColor = System.Drawing.Color.Black;
                    lblPlayerCricketArray[playerNo - 1, i, 2] = lblCricket3;
                    this.formScore.Controls.Add(lblCricket3);

                    var lblCricket4 = new Label();
                    lblCricket4.Location = new System.Drawing.Point(lblPlayerScore.Left + lblPlayerScore.Width + 5 + (29 * i), top + 93 + (117 * (playerNo - 1)));
                    lblCricket4.Size = new System.Drawing.Size(27, 25);
                    lblCricket4.BackColor = System.Drawing.Color.Black;
                    lblPlayerCricketArray[playerNo - 1, i, 3] = lblCricket4;
                    this.formScore.Controls.Add(lblCricket4);
                }
                
            } 
            else 
            {
                // Player Avg 3darts
                var lblPlayerAvg = new Label();
                lblPlayerAvg.BackColor = System.Drawing.Color.Black;
                lblPlayerAvg.Font = new System.Drawing.Font("Comic Sans MS", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                lblPlayerAvg.ForeColor = System.Drawing.Color.Gray;
                lblPlayerAvg.Location = new System.Drawing.Point(lblPlayerScore.Left + lblPlayerScore.Width + 5, top + 9 + 117 * (playerNo - 1));
                lblPlayerAvg.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
                lblPlayerAvg.Name = "labelAvgPlayer1" + playerNo;
                lblPlayerAvg.Size = new System.Drawing.Size(85, 50);
                lblPlayerAvg.Text = "0";
                lblPlayerAvgArray[playerNo - 1] = lblPlayerAvg;
                t = new ToolTip();
                t.SetToolTip(lblPlayerAvg, "3-Darts-Punktedurchschnitt");
                this.formScore.Controls.Add(lblPlayerAvg);

                // Player Avg 1darts
                var lblPlayerAvg2 = new Label();
                lblPlayerAvg2.BackColor = System.Drawing.Color.Black;
                lblPlayerAvg2.Font = new System.Drawing.Font("Comic Sans MS", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                lblPlayerAvg2.ForeColor = System.Drawing.Color.Gray;
                lblPlayerAvg2.Location = new System.Drawing.Point(lblPlayerScore.Left + lblPlayerScore.Width + 5, top + 66 + 117 * (playerNo - 1));
                lblPlayerAvg2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
                lblPlayerAvg2.Name = "labelAvgPlayer1" + playerNo;
                lblPlayerAvg2.Size = new System.Drawing.Size(85, 50);
                lblPlayerAvg2.Text = "0";
                lblPlayerAvg2Array[playerNo - 1] = lblPlayerAvg2;
                t = new ToolTip();
                t.SetToolTip(lblPlayerAvg2, "1-Dart-Punktedurchschnitt");
                this.formScore.Controls.Add(lblPlayerAvg2);

                // Player Top Score 3darts
                var lblPlayerTopScore = new Label();
                lblPlayerTopScore.BackColor = System.Drawing.Color.Black;
                lblPlayerTopScore.Font = new System.Drawing.Font("Comic Sans MS", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                lblPlayerTopScore.ForeColor = System.Drawing.Color.Gray;
                lblPlayerTopScore.Location = new System.Drawing.Point(lblPlayerScore.Left + lblPlayerScore.Width + lblPlayerAvg.Width + 10, top + 9 + 117 * (playerNo - 1));
                lblPlayerTopScore.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
                lblPlayerTopScore.Name = "labelTopScorePlayer1" + playerNo;
                lblPlayerTopScore.Size = new System.Drawing.Size(85, 50);
                lblPlayerTopScore.Text = "0";
                lblPlayerTopScoreArray[playerNo - 1] = lblPlayerTopScore;
                t = new ToolTip();
                t.SetToolTip(lblPlayerTopScore, "3-Darts-TopScore");
                this.formScore.Controls.Add(lblPlayerTopScore);

                // Player Top Score 1darts
                var lblPlayerTopScore2 = new Label();
                lblPlayerTopScore2.BackColor = System.Drawing.Color.Black;
                lblPlayerTopScore2.Font = new System.Drawing.Font("Comic Sans MS", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                lblPlayerTopScore2.ForeColor = System.Drawing.Color.Gray;
                lblPlayerTopScore2.Location = new System.Drawing.Point(lblPlayerScore.Left + lblPlayerScore.Width + lblPlayerAvg2.Width + 10, top + 66 + 117 * (playerNo - 1));
                lblPlayerTopScore2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
                lblPlayerTopScore2.Name = "labelTopScorePlayer1" + playerNo;
                lblPlayerTopScore2.Size = new System.Drawing.Size(85, 50);
                lblPlayerTopScore2.Text = "0";
                lblPlayerTopScore2Array[playerNo - 1] = lblPlayerTopScore2;
                t = new ToolTip();
                t.SetToolTip(lblPlayerTopScore2, "1-Dart-TopScore");
                this.formScore.Controls.Add(lblPlayerTopScore2);
            }
            playerCount += 1;

            this.buttonStart.Enabled = true;
            this.comboBoxGame.Enabled = false;
            this.comboBoxGameOption1.Enabled = false;
            this.comboBoxGameOptionIn.Enabled = false;
            this.comboBoxGameOptionOut.Enabled = false;

            this.resizePlayers();
        }

        private void refreshData()
        {
            refreshLabelArray(lblPlayerArray, true);
            refreshLabelArray(lblPlayerScoreArray, true);
            if (this.comboBoxGame.Text == "A-Cricket")
            {
            }
            else if (this.comboBoxGame.Text == "Half-It 10" || this.comboBoxGame.Text == "Half-It 10 V2" || this.comboBoxGame.Text == "Half-It 20")
            {
                if (this.currentRound < this.halfItMaxRounds)
                {
                    if (this.comboBoxGame.Text == "Half-It 10") 
                    {
                        if (this.currentRound == 2)
                        {
                            this.formScore.getLabelInfo().Text = this.halfItRounds[this.currentRound - 1].ToString() + " -> 2x" + this.halfItRounds[this.currentRound].ToString();
                        }
                        else if (this.currentRound == 3)
                        {
                            this.formScore.getLabelInfo().Text = "2x" + this.halfItRounds[this.currentRound - 1].ToString() + " -> " + this.halfItRounds[this.currentRound].ToString();
                        }
                        else if (this.currentRound == 5)
                        {
                            this.formScore.getLabelInfo().Text = this.halfItRounds[this.currentRound - 1].ToString() + " -> 3x" + this.halfItRounds[this.currentRound].ToString();
                        }
                        else if (this.currentRound == 6)
                        {
                            this.formScore.getLabelInfo().Text = "3x" + this.halfItRounds[this.currentRound - 1].ToString() + " -> " + this.halfItRounds[this.currentRound].ToString();
                        }
                        else
                        {
                            this.formScore.getLabelInfo().Text = this.halfItRounds[this.currentRound - 1].ToString() + " -> " + this.halfItRounds[this.currentRound].ToString();
                        }
                    }
                    else if (this.comboBoxGame.Text == "Half-It 10 V2")
                    {
                        if (this.halfItRoundsX[this.currentRound - 1] == 1)
                        {
                            this.formScore.getLabelInfo().Text = this.halfItRounds[this.currentRound - 1].ToString();
                        }
                        else if (this.halfItRoundsX[this.currentRound - 1] == 2)
                        {
                            this.formScore.getLabelInfo().Text = "2 x " + this.halfItRounds[this.currentRound - 1].ToString();
                        }
                        else if (this.halfItRoundsX[this.currentRound - 1] == 3)
                        {
                            this.formScore.getLabelInfo().Text = "3 x " + this.halfItRounds[this.currentRound - 1].ToString();
                        }
                    }
                    else if (this.comboBoxGame.Text == "Half-It 20")
                    {
                        if (this.currentRound == 16)
                        {
                            this.formScore.getLabelInfo().Text = this.halfItRounds[this.currentRound - 1].ToString() + " -> 2x" + this.halfItRounds[this.currentRound].ToString();
                        }
                        else if (this.currentRound == 17)
                        {
                            this.formScore.getLabelInfo().Text = "2x" + this.halfItRounds[this.currentRound - 1].ToString() + " -> " + this.halfItRounds[this.currentRound].ToString();
                        }
                        else if (this.currentRound == 18)
                        {
                            this.formScore.getLabelInfo().Text = this.halfItRounds[this.currentRound - 1].ToString() + " -> 3x" + this.halfItRounds[this.currentRound].ToString();
                        }
                        else if (this.currentRound == 19)
                        {
                            this.formScore.getLabelInfo().Text = "3x" + this.halfItRounds[this.currentRound - 1].ToString() + " -> " + this.halfItRounds[this.currentRound].ToString();
                        }
                        else
                        {
                            this.formScore.getLabelInfo().Text = this.halfItRounds[this.currentRound - 1].ToString() + " -> " + this.halfItRounds[this.currentRound].ToString();
                        }
                    }
                    
                }
                else if (this.currentRound == this.halfItMaxRounds)
                {
                    if (this.comboBoxGame.Text != "Half-It 10 V2")
                    {
                        this.formScore.getLabelInfo().Text = this.halfItRounds[this.currentRound - 1].ToString() + " -> Bull";
                    }
                    else
                    {
                        if (this.halfItRoundsX[this.currentRound - 1] == 1)
                        {
                            this.formScore.getLabelInfo().Text = this.halfItRounds[this.currentRound - 1].ToString();
                        }
                        else if (this.halfItRoundsX[this.currentRound - 1] == 2)
                        {
                            this.formScore.getLabelInfo().Text = "2 x " + this.halfItRounds[this.currentRound - 1].ToString();
                        }
                        else if (this.halfItRoundsX[this.currentRound - 1] == 3)
                        {
                            this.formScore.getLabelInfo().Text = "3 x " + this.halfItRounds[this.currentRound - 1].ToString();
                        }
                    }
                }
                else
                {
                    this.formScore.getLabelInfo().Text = "Bull";
                }
            }

            else if (this.comboBoxGame.Text == "301 Scream")
            {
                this.formScore.getLabelInfo().Text = "";
                int nextPlayer = this.currentPlayer;
                int currentPlayer = this.currentPlayer;
                for (;;)
                {
                    if (nextPlayer >= this.playerCount)
                    {
                        nextPlayer = 1;
                    }
                    else
                    {
                        nextPlayer += 1;
                    }
                    if (nextPlayer > this.playerCount)
                    {
                        nextPlayer = 1;
                    }
                    if (nextPlayer == currentPlayer) break;
                    if (currentScore[nextPlayer] - currentScore[currentPlayer] > 0)
                    {
                        this.formScore.getLabelInfo().Text = this.formScore.getLabelInfo().Text + this.lblPlayerArray[nextPlayer - 1].Text.Substring(0, 1) + "[" + Convert.ToString(currentScore[nextPlayer] - currentScore[currentPlayer]) + "] ";
                        //this.formScore.getLabelInfo().Text = this.formScore.getLabelInfo().Text + this.lblPlayerArray[nextPlayer - 1].Text + "[" + Convert.ToString(currentScore[nextPlayer] - currentScore[currentPlayer]) + "] ";
                        //this.formScore.getLabelInfo2().Text = this.formScore.getLabelInfo().Text;
                    }
                }
            }
 
            Boolean visible = true;
            if (this.comboBoxGame.Text == "Half-It 10" || this.comboBoxGame.Text == "Half-It 10 V2" || this.comboBoxGame.Text == "Half-It 20" || this.comboBoxGame.Text == "301 Scream")
            {
                visible = false;
            }
            refreshLabelArray(lblPlayerAvgArray, visible);
            refreshLabelArray(lblPlayerAvg2Array, visible);
            refreshLabelArray(lblPlayerTopScoreArray, visible);
            refreshLabelArray(lblPlayerTopScore2Array, visible);
        }

        private void refreshLabelArray(Array array, Boolean visible)
        {
            int count = 1;
            foreach (Label label in array)
            {
                if (count > playerCount) break;

                if (count == currentPlayer)
                {
                    label.Visible = visible;
                    if (playerFinished[currentPlayer] == true)
                    {
                        label.ForeColor = Color.DarkGray;
                        label.BackColor = Color.Gray;
                    }
                    else
                    {
                        label.ForeColor = Color.Lime;
                        label.BackColor = Color.Black;
                    }
                }
                else
                {
                    label.Visible = visible;
                    if (playerFinished[count] == true)
                    {
                        label.ForeColor = Color.Gray;
                        label.BackColor = Color.WhiteSmoke;
                    }
                    else
                    {
                        label.ForeColor = Color.DarkGray;
                        label.BackColor = Color.Black;
                    }
                }
                count += 1;
            }
        }

        public void addScore(int hitScore, String buttonText)
        {
            if (this.comboBoxGame.Text == "A-Cricket")
            {
                addScoreACricket(hitScore, buttonText);
            }
            else if (this.comboBoxGame.Text == "Half-It 10" || this.comboBoxGame.Text == "Half-It 10 V2" || this.comboBoxGame.Text == "Half-It 20")
            {
                addScoreHalfIt(hitScore, buttonText);
            }
            else if (this.comboBoxGame.Text == "301 Scream")
            {
                addScoreScream(hitScore, buttonText);
            }
            else
            {
                addScoreDefault(hitScore, buttonText);
            }
        }

        public void addScoreScream(int hitScore, String buttonText)
        {
            score[currentPlayer, currentRound, currentDart] = hitScore;
            Boolean toHeight = false;

            // to height
            if (currentScore[currentPlayer] + hitScore > 301)
            {
                toHeight = true;
                currentDart = 4;
            }
            else if (currentScore[currentPlayer] + hitScore == 301)
            {
                playerFinished[currentPlayer] = true;
                currentScore[currentPlayer] = 301;
                lblPlayerScoreArray[currentPlayer - 1].Text = ranking.ToString();
                ranking++;
                currentDart = 3;
            }
            else
            {
                if (hitScore == 50 || hitScore == 25)
                {
                    score[currentPlayer, currentRound, currentDart] = hitScore;
                    currentScore[currentPlayer] = currentScore[currentPlayer] + hitScore;
                }
                else if (hitScore != 0)
                {
                    score[currentPlayer, currentRound, currentDart] = hitScore;
                    currentScore[currentPlayer] = currentScore[currentPlayer] + hitScore;
                }
                else if (buttonText.Contains("3 x"))
                {
                    score[currentPlayer, currentRound, currentDart] = hitScore;
                    currentScore[currentPlayer] = currentScore[currentPlayer] + hitScore;
                }
                else if (buttonText.Contains("2 x"))
                {
                    score[currentPlayer, currentRound, currentDart] = hitScore;
                    currentScore[currentPlayer] = currentScore[currentPlayer] + hitScore;
                }
            }

            if (toHeight)
            {
                int s1 = score[currentPlayer, currentRound, 1];
                int s2 = score[currentPlayer, currentRound, 2];
                int s3 = score[currentPlayer, currentRound, 3];
                currentScore[currentPlayer] = currentScore[currentPlayer] - s1 - s2 - s3 + hitScore;
                score[currentPlayer, currentRound, 1] = 0;
                score[currentPlayer, currentRound, 2] = 0;
                score[currentPlayer, currentRound, 3] = 0;
                updateScoreLabel(lblPlayerScoreArray[currentPlayer - 1], Convert.ToInt16(lblPlayerScoreArray[currentPlayer - 1].Text), currentScore[currentPlayer]);
                this.refreshData();
            }
            else if (!playerFinished[currentPlayer])
            {
                // add score to label
                if (Convert.ToInt16(lblPlayerScoreArray[currentPlayer - 1].Text) + currentScore[currentPlayer] > 0)
                {
                    updateScoreLabel(lblPlayerScoreArray[currentPlayer - 1], Convert.ToInt16(lblPlayerScoreArray[currentPlayer - 1].Text), currentScore[currentPlayer]);
                }

                // check player count hit
                for (int i = 1; i < this.playerCount + 1; i++)
                {
                    if (i != this.currentPlayer)
                    {
                        if (currentScore[i] == currentScore[this.currentPlayer])
                        {
                            currentScore[i] = 0;
                            int memPlayer = this.currentPlayer;
                            this.currentPlayer = i;
                            updateScoreLabel(lblPlayerScoreArray[currentPlayer - 1], Convert.ToInt16(lblPlayerScoreArray[currentPlayer - 1].Text), currentScore[currentPlayer]);
                            this.currentPlayer = memPlayer;
                        }
                    }
                }
                currentDart += 1;
            }

            if (!playerFinished[currentPlayer])
            {
                // dart 3 or to height
                if ((currentDart > 3 || toHeight))
                {
                    formScore.getLabelDart1().ForeColor = Color.Red;
                    formScore.getLabelDart2().ForeColor = Color.Red;
                    formScore.getLabelDart3().ForeColor = Color.Red;
                    currentDart = 1;
                    currentPlayer += 1;
                }
            }
            else
            {
                currentDart = 1;
            }

            this.searchNextPlayer(301);
            this.refreshCurrentDart();
            this.refreshData();



        }
        public void addScoreHalfIt(int hitScore, String buttonText)
        {
            if (this.currentRound == this.halfItMaxRounds + 1 && (hitScore == 50 || hitScore == 25))
            {
                score[currentPlayer, currentRound, currentDart] = hitScore;
                currentScore[currentPlayer] = currentScore[currentPlayer] + hitScore;
                this.hitMember = true;
            }
            else
            {
                if (this.comboBoxGame.Text == "Half-It 10 V2")
                {
                    if (this.halfItRoundsX[this.currentRound - 1] == 1 && (this.halfItRounds[this.currentRound - 1] == hitScore || buttonText.Contains("2 x") || buttonText.Contains("3 x")) && hitScore != 0)
                    {
                        score[currentPlayer, currentRound, currentDart] = hitScore;
                        currentScore[currentPlayer] = currentScore[currentPlayer] + hitScore;
                        this.hitMember = true;
                    }
                    else if (this.halfItRoundsX[this.currentRound - 1] == 2  && buttonText.Contains("2 x") && hitScore % 2 == 0 && hitScore / 2 == this.halfItRounds[this.currentRound - 1] && hitScore != 0)
                    {
                        score[currentPlayer, currentRound, currentDart] = hitScore;
                        currentScore[currentPlayer] = currentScore[currentPlayer] + hitScore;
                        this.hitMember = true;
                    }
                    else if (this.halfItRoundsX[this.currentRound - 1] == 3 && buttonText.Contains("3 x") && hitScore % 3 == 0 && hitScore / 3 == this.halfItRounds[this.currentRound - 1] && hitScore != 0)
                    {
                        score[currentPlayer, currentRound, currentDart] = hitScore;
                        currentScore[currentPlayer] = currentScore[currentPlayer] + hitScore;
                        this.hitMember = true;
                    }
                    else
                    {
                        if (this.currentDart == 3 && this.hitMember == false)
                        {
                            currentScore[currentPlayer] = currentScore[currentPlayer] / 2;
                        }
                    }
                }
                else
                {
                    if (this.halfItRounds[this.currentRound - 1] == hitScore && hitScore != 0 && ((this.halfItMaxRounds == 10 && this.currentRound != 3 && this.currentRound != 6) || (this.halfItMaxRounds == 20 && this.currentRound != 17 && this.currentRound != 19)))
                    {
                        score[currentPlayer, currentRound, currentDart] = hitScore;
                        currentScore[currentPlayer] = currentScore[currentPlayer] + hitScore;
                        this.hitMember = true;
                    }
                    else if ((buttonText.Contains("3 x") && hitScore % 3 == 0 && hitScore / 3 == this.halfItRounds[this.currentRound - 1] && hitScore != 0) && ((this.halfItMaxRounds == 10 && this.currentRound != 3) || (this.halfItMaxRounds == 20 && this.currentRound != 17)))
                    {
                        score[currentPlayer, currentRound, currentDart] = hitScore;
                        currentScore[currentPlayer] = currentScore[currentPlayer] + hitScore;
                        this.hitMember = true;
                    }
                    else if ((buttonText.Contains("2 x") && hitScore % 2 == 0 && hitScore / 2 == this.halfItRounds[this.currentRound - 1] && hitScore != 0) && ((this.halfItMaxRounds == 10 && this.currentRound != 6) || (this.halfItMaxRounds == 20 && this.currentRound != 19)))
                    {
                        score[currentPlayer, currentRound, currentDart] = hitScore;
                        currentScore[currentPlayer] = currentScore[currentPlayer] + hitScore;
                        this.hitMember = true;
                    }
                    else
                    {
                        if (this.currentDart == 3 && this.hitMember == false)
                        {
                            currentScore[currentPlayer] = currentScore[currentPlayer] / 2;
                        }
                    }
                }
            } 

            //lblPlayerScoreArray[currentPlayer - 1].Text = currentScore[currentPlayer].ToString();
            if (Convert.ToInt16(lblPlayerScoreArray[currentPlayer - 1].Text) + currentScore[currentPlayer] > 0)
            {
                updateScoreLabel(lblPlayerScoreArray[currentPlayer - 1], Convert.ToInt16(lblPlayerScoreArray[currentPlayer - 1].Text), currentScore[currentPlayer]);
            }

            this.refreshData();

            if (this.currentDart == 3)
            {
                this.currentPlayer += 1;
                if (currentPlayer > this.playerCount)
                {
                    this.currentPlayer = 1;
                    this.nextRound();
                }
                this.currentDart = 1;
                this.hitMember = false;
            }
            else
            {
                this.currentDart += 1;
            }

            if (this.currentRound == this.halfItMaxRounds + 2)
            {
                //int ranking = 1;
                for (int i = 1; i < this.playerCount+1; i++)
                {
                    this.playerFinished[i] = true;
                    //this.lblPlayerScoreArray[currentPlayer - 1].Text = ranking.ToString() + "(" + this.currentScore[currentPlayer] + ")";
                    //this.currentScore[i] = 0;
                    this.currentPlayer = i;
                    this.refreshData();
                }
                dartsBoard.Hide();
                this.scoreButtons(false);
            }
            else
            {
                this.refreshCurrentDart();
                this.refreshData();
            }
            

        }
        public void addScoreACricket(int hitScore, String buttonText)
        {
            int factor = 1;
            currentDart += 1;
            refreshCurrentDart();

            if (hitScore == 50 || hitScore == 25 || (!buttonText.Contains("3 x") && !buttonText.Contains("2 x") && hitScore > 14) || (buttonText.Contains("3 x") && hitScore / 3 > 14) || (buttonText.Contains("2 x") && hitScore / 2 > 14))
            {
                if (buttonText.Contains("2 x"))
                {
                    hitScore = hitScore / 2;
                    factor = 2;
                }
                if (buttonText.Contains("3 x"))
                {
                    hitScore = hitScore / 3;
                    factor = 3;
                }
                if (hitScore == 25)
                {
                    hitScore = 21;
                }
                if (hitScore == 50)
                {
                    hitScore = 21;
                    factor = 2;
                }

                Label l = lblPlayerCricketArray[currentPlayer - 1,hitScore-15,1];
                if (l.BackColor == System.Drawing.Color.Red)
                {
                    l = lblPlayerCricketArray[currentPlayer - 1, hitScore - 15, 2];
                    if (l.BackColor == System.Drawing.Color.Red)
                    {
                        if (factor > 1)
                        {
                            if (lblPlayerCricketArray[currentPlayer - 1, hitScore - 15, 3].BackColor == System.Drawing.Color.Red)
                            {
                                for (int p = 0; p < playerCount; p++)
                                {
                                    if (p != currentPlayer - 1)
                                    {
                                        lblPlayerCricketArray[p, hitScore - 15, 3].BackColor = System.Drawing.Color.Black;
                                        lblPlayerCricketArray[p, hitScore - 15, 2].BackColor = System.Drawing.Color.Black;
                                        lblPlayerCricketArray[p, hitScore - 15, 1].BackColor = System.Drawing.Color.Black;
                                        /*if (lblPlayerCricketArray[p, hitScore - 15, 3].BackColor == System.Drawing.Color.Red)
                                        {
                                            lblPlayerCricketArray[p, hitScore - 15, 3].BackColor = System.Drawing.Color.Black;
                                        }
                                        else if (lblPlayerCricketArray[p, hitScore - 15, 2].BackColor == System.Drawing.Color.Red)
                                        {
                                            lblPlayerCricketArray[p, hitScore - 15, 2].BackColor = System.Drawing.Color.Black;
                                        }
                                        else if (lblPlayerCricketArray[p, hitScore - 15, 1].BackColor == System.Drawing.Color.Red)
                                        {
                                            lblPlayerCricketArray[p, hitScore - 15, 1].BackColor = System.Drawing.Color.Black;
                                        }*/
                                    }
                                }
                            }
                        }
                        if (factor == 1 || factor == 2 || factor == 3)
                        {
                            lblPlayerCricketArray[currentPlayer - 1, hitScore - 15, 3].BackColor = System.Drawing.Color.Red;
                        }
                    }
                    else
                    {
                        if (factor == 1)
                        {
                            lblPlayerCricketArray[currentPlayer - 1, hitScore - 15, 2].BackColor = System.Drawing.Color.Red;
                        }
                        else if (factor == 2 || factor == 3)
                        {
                            lblPlayerCricketArray[currentPlayer - 1, hitScore - 15, 2].BackColor = System.Drawing.Color.Red;
                            lblPlayerCricketArray[currentPlayer - 1, hitScore - 15, 3].BackColor = System.Drawing.Color.Red;
                        }
                    }
                }
                else
                {
                    if (factor == 1)
                    {
                        lblPlayerCricketArray[currentPlayer - 1, hitScore - 15, 1].BackColor = System.Drawing.Color.Red;
                    }
                    else if (factor == 2)
                    {
                        lblPlayerCricketArray[currentPlayer - 1, hitScore - 15, 1].BackColor = System.Drawing.Color.Red;
                        lblPlayerCricketArray[currentPlayer - 1, hitScore - 15, 2].BackColor = System.Drawing.Color.Red;
                    }
                    else if (factor == 3)
                    {
                        lblPlayerCricketArray[currentPlayer - 1, hitScore - 15, 1].BackColor = System.Drawing.Color.Red;
                        lblPlayerCricketArray[currentPlayer - 1, hitScore - 15, 2].BackColor = System.Drawing.Color.Red;
                        lblPlayerCricketArray[currentPlayer - 1, hitScore - 15, 3].BackColor = System.Drawing.Color.Red;
                    }
                }
            }

            // check finish
            Boolean foundOpenField = false;
            for (int i = 0; i < 7; i++)
            {
                for (int j = 1; j < 4; j++)
                {
                    Label l = lblPlayerCricketArray[currentPlayer - 1, i, j];
                    if (l.BackColor != System.Drawing.Color.Red)
                    {
                        foundOpenField = true;
                    }
                }
            }
            if (!foundOpenField)
            {
                playerFinished[currentPlayer] = true;
                currentScore[currentPlayer] = 1;
                currentDart = 4;
                this.refreshData();
            }

            // dart 3
            if (currentDart > 3)
            {
                formScore.getLabelDart1().ForeColor = Color.Red;
                formScore.getLabelDart2().ForeColor = Color.Red;
                formScore.getLabelDart3().ForeColor = Color.Red;
                currentDart = 1;

                // AVG and TopScore
                if (this.comboBoxGame.Text != "A-Cricket")
                {
                    this.calculateStatistics(hitScore);
                }
                currentPlayer += 1;

                // search next player
                searchNextPlayer(1);
            }

            // check all finished
            foundOpenField = false;
            for (int p = 0; p < playerCount; p++)
            {
                for (int i = 0; i < 7; i++)
                {
                    for (int j = 1; j < 4; j++)
                    {
                        Label l = lblPlayerCricketArray[p, i, j];
                        if (l.BackColor != System.Drawing.Color.Red)
                        {
                            foundOpenField = true;
                        }
                    }
                }
            }

            if (!foundOpenField)
            {
                finish = true;
                this.refreshData();
            }
            this.refreshData();

            clickDelay();
            
        }
        public void addScoreDefault(int hitScore, String buttonText)
        {
            foreach (Button b in buttonScoreArray) 
            {
                if (b != null)
                {
                    if (b.Text == buttonText && !b.Enabled)
                    {
                        hitScore = 0;
                        break;
                    }
                }
            }
            
            score[currentPlayer, currentRound, currentDart] = hitScore;
            Boolean toHeight = false;

            // to height
            if (currentScore[currentPlayer] - hitScore < 0)
            {
                toHeight = true;
                currentDart = 4;
            }
            // player finished
            else if ((currentScore[currentPlayer] - hitScore == 0))
            {
                // master in
                if (this.comboBoxGameOptionOut.SelectedItem.Equals("Master") && (!buttonText.Contains("2 x") && buttonText != "50" && !buttonText.Contains("3 x")))
                {
                    toHeight = true;
                }
                // double in
                else if (this.comboBoxGameOptionOut.SelectedItem.Equals("Double") && (!buttonText.Contains("2 x") && buttonText != "50"))
                {
                    toHeight = true;
                }
                // triple in
                else if (this.comboBoxGameOptionOut.SelectedItem.Equals("Triple") && !buttonText.Contains("3 x"))
                {
                    toHeight = true;
                }
                else
                {
                    playerFinished[currentPlayer] = true;
                    currentScore[currentPlayer] = 0;
                    lblPlayerScoreArray[currentPlayer - 1].Text = ranking.ToString();
                    ranking++;
                    currentDart = 3;
                    playWinSound();
                }
            } 
            // master/double out current score < 2
            else if ((this.comboBoxGameOptionOut.SelectedItem.Equals("Master") || this.comboBoxGameOptionOut.SelectedItem.Equals("Double")) && currentScore[currentPlayer] - hitScore < 2)
            {
                toHeight = true;
            }
            // triple out current score < 3
            else if (this.comboBoxGameOptionOut.SelectedItem.Equals("Triple") && currentScore[currentPlayer] - hitScore < 3)
            {
                toHeight = true;
            }

            

            if (toHeight == false)
            {
                playSound(hitScore, toHeight);

                // set current score
                if (playerFinished[currentPlayer] == false && !toHeight)
                {
                    currentScore[currentPlayer] = currentScore[currentPlayer] - hitScore;
                }

                if (playerFinished[currentPlayer] != true)
                {
                    if (Convert.ToInt16(lblPlayerScoreArray[currentPlayer - 1].Text) - currentScore[currentPlayer] > 0)
                    {
                        updateScoreLabel(lblPlayerScoreArray[currentPlayer - 1], Convert.ToInt16(lblPlayerScoreArray[currentPlayer - 1].Text), currentScore[currentPlayer]);
                    }
                    //lblPlayerScoreArray[currentPlayer - 1].Text = currentScore[currentPlayer].ToString();
                }

                currentDart += 1;
                refreshCurrentDart();
            }
            else
            {
                playSound(0, toHeight);

                int s1 = score[currentPlayer, currentRound, 1];
                int s2 = score[currentPlayer, currentRound, 2];
                int s3 = score[currentPlayer, currentRound, 3];
                currentScore[currentPlayer] = currentScore[currentPlayer] + s1 + s2 + s3- hitScore;
                score[currentPlayer, currentRound, 1] = 0;
                score[currentPlayer, currentRound, 2] = 0;
                score[currentPlayer, currentRound, 3] = 0;
                //updateScoreLabel(lblPlayerScoreArray[currentPlayer - 1], Convert.ToInt16(lblPlayerScoreArray[currentPlayer - 1].Text), currentScore[currentPlayer]);
                lblPlayerScoreArray[currentPlayer - 1].Text = currentScore[currentPlayer].ToString();
                this.refreshData();
            }

            // dart 3 or to height
            if (currentDart > 3 || toHeight)
            {
                formScore.getLabelDart1().ForeColor = Color.Red;
                formScore.getLabelDart2().ForeColor = Color.Red;
                formScore.getLabelDart3().ForeColor = Color.Red;
                currentDart = 1;

                // AVG and TopScore
                this.calculateStatistics(hitScore);

                currentPlayer += 1;

                // search next player
                searchNextPlayer(0);

            }

            if (currentRound > 1)
            {
                comboBoxPlayer.Enabled = false;
            }

            clickDelay();

        }

        private void updateScoreLabel(Label l, int start, int end)
        {
            if (start > end)
            {
                for (int i = start; i >= end; i--)
                {
                    l.Text = i.ToString();
                    l.Refresh();
                    System.Threading.Thread.Sleep(500 / (start - end));
                }
            }
            else
            {
                if (end - start > 0)
                {
                    for (int i = start; i <= end; i++)
                    {
                        l.Text = i.ToString();
                        l.Refresh();
                        System.Threading.Thread.Sleep(500 / (end - start));
                    }
                }
            }
        }

        private void clickDelay()
        {
            if (this.finish)
            {
                dartsBoard.Hide();
                this.scoreButtons(false);
            }
            else
            {
                this.scoreButtons(true);
            }
            
            this.buttonUndo.Enabled = true;
            this.refreshData();
        }

        private void searchNextPlayer(int finishScore)
        {
            // searching next player
            for (int i = currentPlayer; i < playerCount + 1; i++)
            {
                if (currentScore[i] != finishScore)
                {
                    currentPlayer = i;
                    break;
                }
                else
                {
                    currentPlayer += 1;
                }
            }
            if (currentPlayer > playerCount)
            {
                // search next player
                for (int i = 1; i < playerCount + 1; i++)
                {
                    if (currentScore[i] != finishScore)
                    {
                        currentPlayer = i;
                        break;
                    }
                    else
                    {
                        currentPlayer += 1;
                    }
                }
                // check all players finished
                for (int i = 1; i < playerCount + 1; i++)
                {
                    if (currentScore[i] != finishScore)
                    {
                        break;
                    }
                    if (i == playerCount)
                    {
                        finish = true;
                    }
                }

                nextRound();
            }
        }

        private void playSound(int hitScore, bool tooHigh)
        {
            SoundPlayer simpleSound = null;
            if (hitScore == 0)
            {
                Random random = new Random();
                int randomNumber = random.Next(1, 4);
                switch (randomNumber)
                {
                    case 1:
                        simpleSound = new SoundPlayer(Dart.Properties.Resources.lacher_01);
                        break;
                    case 2:
                        simpleSound = new SoundPlayer(Dart.Properties.Resources.lacher_02);
                        break;
                    case 3:
                        simpleSound = new SoundPlayer(Dart.Properties.Resources.lacher_03);
                        break;
                    default:
                        simpleSound = new SoundPlayer(Dart.Properties.Resources.lacher_01);
                        break;
                }

                if (tooHigh)
                {
                    simpleSound = new SoundPlayer(Dart.Properties.Resources.computersagtnein);
                }
            }
            else
            {
                if (hitScore >= 50)
                {
                    simpleSound = new SoundPlayer(Dart.Properties.Resources.Sinnlooooooos);
                    simpleSound.Play();
                }                
            }

            if (simpleSound != null)
            {
                simpleSound.Play();
            }
        }

        private void playWinSound()
        {
            SoundPlayer simpleSound = new SoundPlayer();
            
            Random random = new Random();
            int randomNumber = random.Next(1, 4);
            if (randomNumber == 1) simpleSound = new SoundPlayer(Dart.Properties.Resources.applause_1);
            if (randomNumber == 2) simpleSound = new SoundPlayer(Dart.Properties.Resources.applause_2);
            if (randomNumber == 3) simpleSound = new SoundPlayer(Dart.Properties.Resources.applause_3);
            simpleSound.Play();
        }

        private void calculateStatistics(int hitScore)
        {
            // AVG
            int sum = 0;
            int sum2 = 0;
            int summe = score[currentPlayer, currentRound, 1] + score[currentPlayer, currentRound, 2] + score[currentPlayer, currentRound, 3];

            for (int i = 1; i < currentRound + 1; i++)
            {
                for (int j = 1; j < 4; j++)
                {
                    sum += score[currentPlayer, i, j];
                    sum2 += score[currentPlayer, i, j];
                }
            }

            if (currentRound != 0)
            {
                sum = sum / currentRound;
                sum2 = sum2 / currentRound / 3;
            }

            lblPlayerAvgArray[currentPlayer - 1].Text = sum.ToString();
            lblPlayerAvg2Array[currentPlayer - 1].Text = sum2.ToString();

            // Player TopScore 3darts
            if (currentTopScore[currentPlayer] < summe)
            {
                currentTopScore[currentPlayer] = summe;
                lblPlayerTopScoreArray[currentPlayer - 1].Text = currentTopScore[currentPlayer].ToString();
            }
            // Player TopScore 1dart
            if (currentTopScore2[currentPlayer] < hitScore)
            {
                currentTopScore2[currentPlayer] = hitScore;
                lblPlayerTopScore2Array[currentPlayer - 1].Text = currentTopScore2[currentPlayer].ToString();
            }

            this.dataGridViewInfo.Rows.Add(new object[] { lblPlayerArray[currentPlayer - 1].Text, score[currentPlayer, currentRound, 1], score[currentPlayer, currentRound, 2], score[currentPlayer, currentRound, 3], summe, sum, sum2 });
            this.dataGridViewInfo.CurrentCell = this.dataGridViewInfo[0, this.dataGridViewInfo.Rows.Count - 1];

            dataTableScore.Rows.Add(new Object[] { System.DateTime.Now, lblPlayerArray[currentPlayer - 1].Tag, score[currentPlayer, currentRound, 1], score[currentPlayer, currentRound, 2], score[currentPlayer, currentRound, 3], currentRound, this.comboBoxGame.Text, this.currentScore[currentPlayer] });
                
        }

        private void refreshCurrentDart()
        {
            if (currentDart == 1) { formScore.getLabelDart1().ForeColor = Color.Red; formScore.getLabelDart2().ForeColor = Color.Red; formScore.getLabelDart3().ForeColor = Color.Red; }
            if (currentDart == 2) { formScore.getLabelDart1().ForeColor = Color.Green; formScore.getLabelDart2().ForeColor = Color.Red; formScore.getLabelDart3().ForeColor = Color.Red; }
            if (currentDart == 3) { formScore.getLabelDart1().ForeColor = Color.Green; formScore.getLabelDart2().ForeColor = Color.Green; formScore.getLabelDart3().ForeColor = Color.Red; }
        }

        private void nextRound()
        {
            currentRound += 1;
            this.refreshRound();
            this.refreshData();
        }

        private void refreshRound()
        {
            formScore.getRoundLabel().Text = "Runde " + currentRound;
        }

        private void createInputs()
        {
            int count = 0;
            int countAll = 0;
            int height = 30;
            int width = 90;
            int top = 0;
            int left = 3;
            Button buttonScore;
            for (int col = 0; col < 3; col++)
            {
                for (int i = 1; i < 21; i++)
                {
                    buttonScore = new Button();
                    if (col > 0)
                    {
                        buttonScore.Text = col+1 + " x " + i.ToString();
                    }
                    else
                    {
                        buttonScore.Text = i.ToString();
                    }
                    buttonScore.Font = new System.Drawing.Font("Comic Sans MS", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    buttonScore.Size = new System.Drawing.Size(width, height);
                    buttonScore.Location = new System.Drawing.Point(left + (width * col), top + (height * (count)));
                    buttonScore.Tag = (col+1) * i;
                    buttonScoreArray[countAll] = buttonScore;
                    this.panelSettings.Controls.Add(buttonScore);
                    buttonScoreArray[countAll].Click += new System.EventHandler(ClickScoreButton);
                    count += 1;
                    countAll +=1;
                }
                count = 0;
            }
            buttonScore = new Button();
            buttonScore.Font = new System.Drawing.Font("Comic Sans MS", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            buttonScore.Size = new System.Drawing.Size(width, height);
            buttonScore.Location = new System.Drawing.Point(left + (width * 0), top + (height * (20)));
            buttonScore.Tag = 25;
            buttonScoreArray[countAll] = buttonScore;
            buttonScore.Text = "25";
            this.panelSettings.Controls.Add(buttonScore);
            buttonScoreArray[countAll].Click += new System.EventHandler(ClickScoreButton);
            countAll += 1;

            buttonScore = new Button();
            buttonScore.Font = new System.Drawing.Font("Comic Sans MS", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            buttonScore.Size = new System.Drawing.Size(width, height);
            buttonScore.Location = new System.Drawing.Point(left + (width * 1), top + (height * (20)));
            buttonScore.Tag = 50;
            buttonScoreArray[countAll] = buttonScore;
            buttonScore.Text = "50";
            this.panelSettings.Controls.Add(buttonScore);
            buttonScoreArray[countAll].Click += new System.EventHandler(ClickScoreButton);
            countAll += 1;

            buttonScore = new Button();
            buttonScore.Font = new System.Drawing.Font("Comic Sans MS", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            buttonScore.Size = new System.Drawing.Size(width, height);
            buttonScore.Location = new System.Drawing.Point(left + (width * 2), top + (height * (20)));
            buttonScore.Tag = 0;
            buttonScoreArray[countAll] = buttonScore;
            buttonScore.Text = "0";
            this.panelSettings.Controls.Add(buttonScore);
            buttonScoreArray[countAll].Click += new System.EventHandler(ClickScoreButton);
        }

        private void scoreButtons(Boolean enable)
        {
            foreach (Button b in buttonScoreArray)
            {
                if (b != null)
                {
                    // enable double in
                    if (enable && this.comboBoxGameOptionIn.SelectedItem.Equals("Double") && currentScore[currentPlayer] == Convert.ToInt16(this.comboBoxGame.Text) && (b.Text.Contains("2 x") || b.Text == "0" || b.Text == "50"))
                    {
                        b.Enabled = true;
                    }
                    else if (enable && this.comboBoxGameOptionIn.SelectedItem.Equals("Double") && currentScore[currentPlayer] == Convert.ToInt16(this.comboBoxGame.Text) && !b.Text.Contains("2 x"))
                    {
                        b.Enabled = false;
                    }
                    // enable triple in
                    else if (enable && this.comboBoxGameOptionIn.SelectedItem.Equals("Triple") && currentScore[currentPlayer] == Convert.ToInt16(this.comboBoxGame.Text) && (b.Text.Contains("3 x") ||b.Text == "0"))
                    {
                        b.Enabled = true;
                    }
                    else if (enable && this.comboBoxGameOptionIn.SelectedItem.Equals("Triple") && currentScore[currentPlayer] == Convert.ToInt16(this.comboBoxGame.Text) && !b.Text.Contains("3 x"))
                    {
                        b.Enabled = false;
                    }
                    // enable master in
                    else if (enable && this.comboBoxGameOptionIn.SelectedItem.Equals("Master") && currentScore[currentPlayer] == Convert.ToInt16(this.comboBoxGame.Text) && (b.Text.Contains("3 x") || b.Text.Contains("2 x") || b.Text == "0" || b.Text == "50"))
                    {
                        b.Enabled = true;
                    }
                    else if (enable && this.comboBoxGameOptionIn.SelectedItem.Equals("Master") && currentScore[currentPlayer] == Convert.ToInt16(this.comboBoxGame.Text) && (!b.Text.Contains("3 x") || !b.Text.Contains("2 x")))
                    {
                        b.Enabled = false;
                    }
                    else
                    {
                        b.Enabled = enable;
                    }
                }
            }
        }

        private void ClickScoreButton(object sender, EventArgs e)
        {
            Button s = (Button)sender;
            int hit = Convert.ToInt16(s.Tag.ToString());
            if (this.comboBoxGame.Text == "A-Cricket")
            {
                addScoreScream(hit, s.Text);
            }
            else
            {
                addScore(hit, s.Text);
            }
        }

        private void buttonUndo_Click(object sender, EventArgs e)
        {
            
            if (currentDart == 1)
            {
                if (currentPlayer == 1)
                {
                    for (int i = playerCount; i > 1; i--)
                    {
                        if (!playerFinished[i])
                        {
                            currentPlayer = i;
                            currentRound -= 1;
                            currentDart = 3;
                            break;
                        }
                    }

                }
                else
                {
                    currentPlayer -= 1;
                    currentDart = 3;
                }
            }
            else
            {
                currentDart -= 1;
            }

            currentScore[currentPlayer] += score[currentPlayer, currentRound, currentDart];
            score[currentPlayer, currentRound, currentDart] = 0;
            lblPlayerScoreArray[currentPlayer - 1].Text = currentScore[currentPlayer].ToString();

            if (this.playerFinished[currentPlayer] == true)
            {
                this.playerFinished[currentPlayer] = false;
            }

            this.refreshData();
            this.refreshCurrentDart();
            this.refreshRound();

            if (currentDart == 1 && currentRound == 1 && currentPlayer == 1)
            {
                buttonUndo.Enabled = false;
            }
        }

        private void comboBoxPlayer_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!formScore.Visible)
            {
                formScore.Show(this);
            }

            if (currentRound < 2)
            {
                addPlayer(playerCount + 1, this.comboBoxGame.Text, this.comboBoxPlayer.Text, this.comboBoxPlayer.Tag.ToString());
                if (playerCount == maxPlayers)
                {
                    this.comboBoxPlayer.Enabled = false;
                }
            }

            this.buttonReset.Enabled = true;
        }

        private void comboBoxGame_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBoxGame.Text == "A-Cricket" || this.comboBoxGame.Text == "Half-It 10" || this.comboBoxGame.Text == "Half-It 10 V2" || this.comboBoxGame.Text == "Half-It 20" || this.comboBoxGame.Text == "301 Scream")
            {
                comboBoxGameOptionIn.Enabled = false;
                comboBoxGameOptionOut.Enabled = false;
            }
            else
            {
                comboBoxGameOptionIn.Enabled = true;
                comboBoxGameOptionOut.Enabled = true;
            }
        }

        private void panelSesttings_Paint(object sender, PaintEventArgs e)
        {

        }

    }
}
